import Reflux from 'reflux'
import { PriceAction } from '../actions'
import { config, helper, myFetch } from '../components/utils'
import math from 'mathjs'

class PriceStore extends Reflux.Store {
	constructor() {
		super()
		this.first = true
		this.firstPrice = true
		this.listenables = PriceAction
	}
	onChangePrice(status, number = 0) {
		if (!this.timerprice&& !this.firstPrice) {return}
		if (this.firstPrice) {
			this.firstPrice = false
		}
		//               console.log(status, 10)
		const options = {
			method: 'POST'
		}
		myFetch(`${config.rootApi}/mRatelist`, options, true)
		 .then((rs) => {
		 	if (rs && rs.dataInfoRate && rs.dataInfoRate.dataListRates) {
		 		let ladderSpreadBid = 0, ladderSpreadAsk = 0, sellPrice, buyPrice, length = 5
		 		let init = JSON.parse(localStorage.init)
		 		//console.log(init, 'init')
		 		console.log(status)
		 		if (status.includes('JPY')) {
			      length = 3
			    }
		 		let initfilter = []
		 		initfilter = init.filter((val, index) => {
		 			return val.ccyPair == status
		 		})
		 		//console.log(initfilter, '1')
		 		let data = rs.dataInfoRate.dataListRates
		 		let result = []
		 		result = data.filter((val, index) => {
		 			return val.ccyPair == status
		 		})
		 		if (number == 0) {
		 			ladderSpreadBid = 0;
		 			ladderSpreadAsk = 0;
		 			sellPrice = helper.accAdd(result[0].bidPriceFix, initfilter[0].markupValueBid)
		 			buyPrice = helper.accAdd(result[0].askPriceFix, initfilter[0].markupValueAsk)
		 		} else {
		 			let ladderarr = initfilter[0].dataInfoLadder.dataListLadders
		 			let newladder = ladderarr.filter((val, index) => {
		 				if (number >= val.orderAmtLower && number <= val.orderAmtUpper ) {
		 					return true
		 				}
		 			})
		 			//console.log(parseFloat(initfilter[0].markupValueBid), 10)
		 			ladderSpreadBid = newladder[0].ladderSpreadBid
		 			ladderSpreadAsk = newladder[0].ladderSpreadAsk
		 			//console.log(parseFloat(ladderSpreadBid), 11)
		 			//console.log(result[0].bidPriceFix)
		 			if (newladder[0].fixFlag == 1) {
		 		sellPrice = math.chain(parseFloat(ladderSpreadBid))
		 						.add(parseFloat(initfilter[0].markupValueBid))
		 						.add(parseFloat(result[0].bidPriceFix))
		 						.done()
		 		buyPrice = math.chain(parseFloat(ladderSpreadAsk))
		 					   .add(parseFloat(initfilter[0].markupValueAsk))
		 					   .add(parseFloat(result[0].askPriceFix))
		 					   .done()
		 			} else if (newladder[0].fixFlag == 2) {
		 		sellPrice = math.chain(parseFloat(ladderSpreadBid))	
		 						.add(parseFloat(initfilter[0].markupValueBid))
		 						.add(parseFloat(result[0].bidPriceVar))
		 						.done()
		 		buyPrice = math.chain(parseFloat(ladderSpreadAsk))
		 					   .add(parseFloat(initfilter[0].markupValueAsk))
		 					   .add(parseFloat(result[0].askPriceVar))
		 					   .done()
		 			}
		 			console.log(sellPrice, 13)
		 			console.log(buyPrice, 14)
		 			//console.log(parseFloat(initfilter[0].markupValueBid).toString(),15)
		 		}
		 		//console.log(ladderSpreadBid, 'ladderSpreadBid')
		 		let highPrice
		 		let lowPrice
		 		let openPrice, askPriceVar, bidPriceVar
		 		
		 		highPrice = result[0].highPriceBid
		 		lowPrice = result[0].lowPriceBid
		 		openPrice = result[0].openPriceBid
		 		askPriceVar = result[0].askPriceVar
		 		bidPriceVar = result[0].bidPriceVar
		 		//console.log(sellPrice, buyPrice, 1)
		 		this.setState({
		 			sellPrice: Number(sellPrice).toFixed(length),
		 		    buyPrice: Number(buyPrice).toFixed(length),
		 		    highPrice, lowPrice, openPrice, bidPriceVar, askPriceVar
		 		})
		        if (this.timerprice) {
		        	//console.log(2)
		         	clearTimeout(this.timerprice)
		        }
		        if (this.timer) {
		        	//console.log(1)
		         	clearTimeout(this.timer)
		        }
		 		this.timerprice = setTimeout(() => {
		 			PriceAction.changePrice(status, number)
		 		}, 5000)
		 	}
		 })
	}
	onClearTimeout() {
		console.log(this.timerprice, 'this.timerprice')
		this.timerprice && clearTimeout(this.timerprice)
		this.timerprice = undefined
		this.firstPrice = true
	}
	onInforAll() {
		if (!this.timer && !this.first) {return}
		if (this.first) {
			this.first = false
		}
		
		const options = {
	      method: 'POST'
	    }
	    myFetch(`${config.rootApi}/mRatelist`, options, true)
	     .then((rs) => {
	       if (rs && rs.dataInfoRate && rs.dataInfoRate.dataListRates) {
	         if (!localStorage.ccy) {
	           return
	         }
	         let ccy = JSON.parse(localStorage.ccy)
	         //console.log(ccy.length)

	         let obj = {}
	         rs.dataInfoRate.dataListRates.forEach((val, index) => {
	           obj[val.ccyPair] = val
	         })
	         let init = JSON.parse(localStorage.init)
	 		 //console.log(init, 'init')
	 		 let initfilter = {}
	 		 init.forEach((val, index) => {
	 			let obj = {}
	 			obj.markupValueBid = val.markupValueBid
	 			obj.markupValueAsk = val.markupValueAsk
	 			initfilter[val.ccyPair] = obj
	 		 })
	         let arr = []
	         ccy.forEach((val, index) => {
	           arr.push(Object.assign({}, obj[val.ccyPair], initfilter[val.ccyPair]))
	         })
	         
	         //console.log(obj, 'obj')
	         //console.log(arr, 'arr')
	         //console.log(ccy, 'ccy')
	         //console.log(rs.dataInfoRate.dataListRates)
	         //console.log(2000)
	         if (arr.length < 1) {
	           return
	         }
	         this.setState({
	           dataRatelist: arr
	         })
	         //console.log(arr, 'arr')
	         if (this.timer) {
	         	console.log(3)
	         	clearTimeout(this.timer)
	         	this.timer = undefined
	         }
	         if (this.timerprice) {
	         	console.log(4)
	         	clearTimeout(this.timerprice)
	         }
	         this.timer = setTimeout(() => {
	         	PriceAction.inforAll()
	         }, 1000)
	       }
	     })
	}
	onClearInfor() {
		console.log(this.timer, 'this.timer')
		this.timer && clearTimeout(this.timer)
		this.timer = undefined
		this.first = true
	}
}

export default PriceStore